# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.collectionDao import CollectionDao

from . import app

CORS(app, supports_credentials=True)

'''收藏车 zzh_collection API'''


@app.route('/add-collection', methods=['POST'])
def add_col():
    if not request.json:
        abort(400)
    col_info = {
        "user_id": request.json['user_id'],
        "item_id": request.json['item_id'],
        "collection_status": request.json['collection_status']
    }

    rs = CollectionDao().add_collection(col_info)
    if rs:
        return jsonify({"colInfo": col_info, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/query-collection/<int:user_id>', methods=['GET'])
def query_col(user_id):
    rs = CollectionDao().query_collections(user_id)
    if rs:
        return jsonify({"colInfo": rs, "message": {"code": 1, "info": "OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "ERROR"}})


# --------批量删除收藏车列表API------------
@app.route('/delete-collections', methods=['POST'])
def delete_cols():
    if not request.json:
        abort(400)
    ids = []
    ids = request.json['collection_ids']
    CollectionDao().delete_cols(ids)

    return jsonify({"message": {"code": 1, "info": "OK"}})


# 批量添加收藏车，根据传入的userId和itemId,然后将这两个参数多次插入zzh_collection收藏车表
@app.route('/add-many-collections', methods=['POST'])
def addManyCollections():
    userId = request.json["userId"]
    itemIds = request.json["itemId"]

    addManyCollectionsResult = CollectionDao().addManyCollections(userId, itemIds)

    if addManyCollectionsResult:
        return jsonify({"message": {"code": 1, "info": "批量添加OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "批量添加ERROR"}})


# 逻辑删除收藏车。根据传入的userId和itemId,然后将这两个参数符合的记录中collection_status变成-1
@app.route('/delete-collection-new', methods=['POST'])
def logicDeleteCollection():
    userId = request.json["userId"]
    itemId = request.json["itemId"]

    logicDeleteCollectionsResult = CollectionDao().logicDeleteCollection(userId, itemId)

    if logicDeleteCollectionsResult:
        return jsonify({"message": {"code": 1, "info": "逻辑删除收藏车OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "逻辑删除收藏车  ERROR"}})

